package com.smedi.ismedi.dangeroussourcemanage.core.application.query.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.util.AuthorityUtil;
import com.smedi.ismedi.dangeroussourcemanage.api.dto.request.DangerSourceListRequest;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.DangerousSourceListCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.MobileQueryDangerousSourceListCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.QueryDangerousSourcePageListCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.TimeLineCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.application.convert.AppConverter;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.DangerousSourceManageQueryService;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerSourceVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceLibraryVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceListVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceManageDetailQueryVo;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.*;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceLibraryRepository;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceManageRepository;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.model.DangerousSourceManageEntity;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.smedi.ismedi.dangeroussourcemanage.core.infrastructure.constant.ConstantField.ACCESS_PROJECT;

/**
 * 危险源查询实现类
 *
 * @author xienannan
 * @date 2021/08/23
 */
@Service
@AllArgsConstructor
public class DangerousSourceManageQueryServiceImpl implements DangerousSourceManageQueryService {
    private final DangerousSourceManageRepository dangerousSourceManageRepository;

    private final DangerousSourceLibraryRepository dangerousSourceLibraryRepository;

    private final DozerUtil dozerUtil;

    private final AuthorityUtil authorityUtil;

    private final AppConverter appConverter;

    @Override
    public Page<DangerousSourceListVo> postQueryDangerousSourceManagePageList(QueryDangerousSourcePageListCommand queryDangerousSourcePageListCommand) {
        QueryDangerousSourceManagePageList queryDangerousSourceManagePageList = dozerUtil.convertor(queryDangerousSourcePageListCommand, QueryDangerousSourceManagePageList.class);
        Page<DangerousSourceListVo> dangerousSourceListVoPage = new Page<>();
        Page<DangerousSourceManage> dangerousSourceManagePage = dangerousSourceManageRepository.selectDangerousSourcePageList(queryDangerousSourceManagePageList);
        if (dangerousSourceManagePage == null) {
            return null;
        }

        List<DangerousSourceListVo> dangerousSourceListVoList = new ArrayList<>();

        for (DangerousSourceManage dangerousSourceManage : dangerousSourceManagePage.getRecords()) {
            DangerousSourceListVo dangerousSourceListVo = dozerUtil.convertor(dangerousSourceManage, DangerousSourceListVo.class);
            dangerousSourceListVo.setDangerousSourceLibraryVo(dozerUtil.convertor(dangerousSourceManage.getDangerousSourceLibrary(), DangerousSourceLibraryVo.class));
            dangerousSourceListVoList.add(dangerousSourceListVo);
        }

        dangerousSourceListVoPage.setRecords(dangerousSourceListVoList);
        dangerousSourceListVoPage.setTotal(dangerousSourceManagePage.getTotal());

        return dangerousSourceListVoPage;
    }

    @Override
    public List<DangerousSourceListVo> postQueryDangerousSourceManageList(DangerousSourceListCommand dangerousSourceListCommand) {
        QueryDangerousSourceList queryDangerousSourceList = dozerUtil.convertor(dangerousSourceListCommand, QueryDangerousSourceList.class);
        List<DangerousSourceManage> dangerousSourceManageList = dangerousSourceManageRepository.selectDangerousSourceList(queryDangerousSourceList);
        if (dangerousSourceManageList == null) {
            return null;
        }
        List<DangerousSourceListVo> dangerousSourceListVoList = new ArrayList<>();
        for (DangerousSourceManage dangerousSourceManage : dangerousSourceManageList) {
            DangerousSourceListVo dangerousSourceListVo = dozerUtil.convertor(dangerousSourceManage, DangerousSourceListVo.class);
            dangerousSourceListVo.setDangerousSourceLibraryVo(dozerUtil.convertor(dangerousSourceManage.getDangerousSourceLibrary(), DangerousSourceLibraryVo.class));
            dangerousSourceListVoList.add(dangerousSourceListVo);
        }
        return dangerousSourceListVoList;
    }

    @Override
    public DangerousSourceManageDetailQueryVo getQueryDangerousSourceManageDetail(long dangerousSourceId) {
        DangerousSourceManage dangerousSourceManage = dangerousSourceManageRepository.doSelectById(dangerousSourceId);
        if (dangerousSourceManage == null) {
            return null;
        }

        DangerousSourceManageDetailQueryVo dangerousSourceManageDetailQueryVo = dozerUtil.convertor(dangerousSourceManage, DangerousSourceManageDetailQueryVo.class);
        dangerousSourceManageDetailQueryVo.setDangerousSourceLibraryVo(dozerUtil.convertor(dangerousSourceManage.getDangerousSourceLibrary(), DangerousSourceLibraryVo.class));

        return dangerousSourceManageDetailQueryVo;
    }

    @Override
    public Page<DangerousSourceListVo> mobileQueryDangerousSourceManagePageList(MobileQueryDangerousSourceListCommand mobileQueryDangerousSourceListCommand) {
        MobileQueryDangerousSourceManageList mobileQueryDangerousSourceManageList = dozerUtil.convertor(mobileQueryDangerousSourceListCommand, MobileQueryDangerousSourceManageList.class);
        Page<DangerousSourceListVo> dangerousSourceListVoPage = new Page<>();
        Page<DangerousSourceManage> dangerousSourceManagePage = dangerousSourceManageRepository.selectMobileDangerousSourceList(mobileQueryDangerousSourceManageList);
        if (dangerousSourceManagePage == null) {
            return null;
        }

        List<DangerousSourceListVo> dangerousSourceListVoList = new ArrayList<>();

        for (DangerousSourceManage dangerousSourceManage : dangerousSourceManagePage.getRecords()) {
            DangerousSourceListVo dangerousSourceListVo = dozerUtil.convertor(dangerousSourceManage, DangerousSourceListVo.class);
            dangerousSourceListVo.setDangerousSourceLibraryVo(dozerUtil.convertor(dangerousSourceManage.getDangerousSourceLibrary(), DangerousSourceLibraryVo.class));
            dangerousSourceListVoList.add(dangerousSourceListVo);
        }

        dangerousSourceListVoPage.setRecords(dangerousSourceListVoList);
        dangerousSourceListVoPage.setTotal(dangerousSourceManagePage.getTotal());

        return dangerousSourceListVoPage;
    }

    @Override
    public List<DangerSourceVo> getDangerSourceList(DangerSourceListRequest dangerSourceListRequest) {
        List<DangerousSourceManageEntity> dangerousSourceManageEntities = dangerousSourceManageRepository.getProjectWithDangerSourceList(dangerSourceListRequest);
        Map<Long, String> idNameMap = dangerousSourceLibraryRepository.getIdNameMap(dangerousSourceManageEntities.stream().map(DangerousSourceManageEntity::getLibraryId).collect(Collectors.toList()));
        List<DangerSourceVo> dangerSourceVos = dozerUtil.convertor(dangerousSourceManageEntities, DangerSourceVo.class);
        for (DangerSourceVo dangerSourceVo:dangerSourceVos){
            dangerSourceVo.setName(idNameMap.get(dangerSourceVo.getLibraryId()));
        }
        return dangerSourceVos;
    }

    @Override
    public Integer getDangerSourceCount(DangerSourceListRequest dangerSourceListRequest) {
        return dangerousSourceManageRepository.getProjectWithDangerSourceCount(dangerSourceListRequest);
    }

    @Override
    public Integer getDangerSourceCountByTimeLine(TimeLineCommand timeLineCommand) {
        TimeLine timeLine = appConverter.timeLineCommandToTimeLine(timeLineCommand);
        return dangerousSourceManageRepository.getDangerSourceCountByTimeLine(timeLine);
    }
}
